using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace OneSys.Misc
{
    /// <summary>
    /// Interaction logic for OneSysAbout.xaml
    /// </summary>
    public partial class OneSysAbout : Window
    {
        AboutBox about = null;
        public OneSysAbout( Uri center, Uri topLeft, CultureInfo cultureInfo)
        {
            Thread.CurrentThread.CurrentUICulture = cultureInfo;
            this.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            InitializeComponent();
            about = new AboutBox(ToBitMap(center), ToBitMap(topLeft));
            this.Title = about.Text;
            this.Height = about.Height;
            this.Width = about.Width;
            this.Icon = ToImageSource(about.AppIcon);
            this.ShowInTaskbar = false;
        }

        public OneSysAbout( Uri center, Uri topLeft)
        {
            this.Owner = Application.Current.Windows.OfType<Window>().SingleOrDefault(x => x.IsActive);
            this.WindowStartupLocation = WindowStartupLocation.CenterOwner;
            InitializeComponent();
            about = new AboutBox(ToBitMap(center), ToBitMap(topLeft));
            this.Title = about.Text;
            this.Height = about.Height;
            this.Width = about.Width;
            this.Icon = ToImageSource(about.AppIcon);
            this.ShowInTaskbar = false;
        }

        private static System.Drawing.Image ToBitMap(Uri img)
        {
            if(img == null)
            {
                return null;
            }   
            BitmapImage image = new BitmapImage(img);

            // copy to byte array
            int stride = image.PixelWidth * 4;
            byte[] buffer = new byte[stride * image.PixelHeight];
            image.CopyPixels(buffer, stride, 0);

            // create bitmap
            System.Drawing.Bitmap bitmap =
                new System.Drawing.Bitmap(
                    image.PixelWidth,
                    image.PixelHeight,
                    System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            // lock bitmap data
            System.Drawing.Imaging.BitmapData bitmapData =
                bitmap.LockBits(
                    new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height),
                    System.Drawing.Imaging.ImageLockMode.WriteOnly,
                    bitmap.PixelFormat);

            // copy byte array to bitmap data
            System.Runtime.InteropServices.Marshal.Copy(
                buffer, 0, bitmapData.Scan0, buffer.Length);

            // unlock
            bitmap.UnlockBits(bitmapData);


            return bitmap;
        }

        private static ImageSource ToImageSource(Icon icon)
        {
            ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
                icon.Handle,
                Int32Rect.Empty,
                BitmapSizeOptions.FromEmptyOptions());

            return imageSource;
        }

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
            var host = new System.Windows.Forms.Integration.WindowsFormsHost();

            // Create the MaskedTextBox control.
           
            about.TopLevel = false;
            about.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            // Assign the MaskedTextBox control as the host control's child.
            host.Child = about;
            // Add the interop host control to the Grid
            // control's collection of child controls.
            this.aboutGrid.Children.Add(host);

    
        }
    }
}
